﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PersonEntity;
using PersonProject;

public partial class _Default : System.Web.UI.Page
{
    PersonProject.Person p=new PersonProject.Person();
    DS_Entity ds = new DS_Entity();
    public static List<PersonProject.Person> pList;
    public string command;
    int SelectedID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        EditPanel.Visible = false;
        Btn_load_Click(this, new EventArgs());
    }
    protected void Btn_load_Click(object sender, EventArgs e)
    {
        pList = ds.Load();
        Person_Grid0.DataSource = pList;        
        Person_Grid0.DataBind();
    }

    protected void Btn_edit_Click(object sender, EventArgs e)
    {
        EditPanel.Visible = true;
        EmptyFill();
        Button btn = (Button)sender;
        command = btn.Text;
        if (btn.Text == "Edit")
        {
            SelectedID = Convert.ToInt32(TextBox_selected0.Text);
            foreach (PersonProject.Person pp in pList)
            {
                if (pp != null)
                {
                    if (SelectedID == pp.Id)
                    {
                        p = pp;
                        TextBox_id.Text = p.Id.ToString();
                        TextBox_name.Text = p.FName;
                        TextBox_add1.Text = p.LName;
                        TextBox_add2.Text = p.Age.ToString();
                        AdressGrid.DataSource = p.Adresses;
                        ContactGrid.DataSource = p.Contacts;
                        AdressGrid.DataBind();
                        ContactGrid.DataBind();
                    }
                }
            }
        }
        else if (btn.Text == "New")
        {
            TextBox_selected0.Text = "";
        }
    }
        public void EmptyFill()
        {
            p = new PersonProject.Person(0);
            TextBox_id.Text = "";
            TextBox_name.Text = "";
            TextBox_add1.Text = "";
            TextBox_add2.Text = "";
            Person_Grid0.DataSource = pList;
            AdressGrid.DataSource = p.Adresses;
            ContactGrid.DataSource = p.Contacts;
            AdressGrid.DataBind();
            ContactGrid.DataBind();
        }

        protected void Btn_OK_Click(object sender, EventArgs e)
        {
            if (TextBox_selected0.Text != "")
            {
                SelectedID = Convert.ToInt32(TextBox_selected0.Text);
            }
            p.Id = Convert.ToInt32(TextBox_id.Text);
            p.FName = TextBox_name.Text;
            p.LName = TextBox_add1.Text;
            p.Age = Convert.ToInt32(TextBox_add2.Text);
            ds.Update(p);
            EditPanel.Visible = false;
            Btn_load_Click(this, new EventArgs());
        }

    protected void Btn_Cancel_Click(object sender, EventArgs e)
    {
        EditPanel.Visible = false;
    }
    protected void Btn_delete_Click(object sender, EventArgs e)
    {
        if (TextBox_selected0.Text != "")
        {
            SelectedID = Convert.ToInt32(TextBox_selected0.Text);
            ds.Delete(SelectedID);            
            Btn_load_Click(this, new EventArgs());
        }
    }
    protected void Person_Grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Person_Grid0.PageIndex = e.NewPageIndex;
        Page_Load(sender, e);
    }
    protected void Person_Grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView temp = (GridView)sender;
        TextBox_selected0.Text = temp.Rows[e.NewEditIndex].Cells[1].Text;
        Btn_edit_Click(Btn_edit0, e);
    }
    protected void Add_Click(object sender, EventArgs e)
    {
        Button pressedBtn = (Button)sender;
        if (pressedBtn.ID == "AddressAdd")
        {
            EditPanel.Visible = true;
            p.Adresses.Add(new PersonProject.Adress());

          
        }
        else 
        {
            EditPanel.Visible = true;
            p.Contacts.Add(new PersonProject.Contact());
        }
        AdressGrid.DataSource = p.Adresses;
        ContactGrid.DataSource = p.Contacts;
        AdressGrid.DataBind();
        ContactGrid.DataBind();
    }
    protected void AdressGrid_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
       
        GridView temp = (GridView)sender;
        
        TextBox_selected0.Text = temp.Rows[e.NewEditIndex].Cells[1].Text;
        EditPanel.Visible = true;      
    }


    protected void Grid_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        GridView temp = (GridView)sender;
     
    }
}